package cn.sumpu.app.wlcity.web.mvc.vote;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import cn.sumpu.app.wlcity.biz.VoteBiz;
import cn.sumpu.app.wlcity.domain.PageModel;
import cn.sumpu.app.wlcity.domain.Vote;
import cn.sumpu.app.wlcity.domain.VoteItem;
import cn.sumpu.app.wlcity.exception.AppBusinessException;
import cn.sumpu.app.wlcity.util.Common;
import cn.sumpu.app.wlcity.util.IdGenerator;
import cn.sumpu.app.wlcity.web.util.ImageUtil;
import cn.sumpu.app.wlcity.web.util.PictureUrl;

@Controller
public class VoteController {
	private static Logger logger = LoggerFactory
			.getLogger(VoteController.class);
	@Autowired
	private VoteBiz voteBiz;

	@RequestMapping(value = "/toVote.cms")
	public String toVote(HttpServletRequest request) {
		request.setAttribute("app", Common.APP_VOTE);  
		return "cms/vote/vote";
	}
	
	@RequestMapping(value = "/toAddVote.cms")
	public String toAddVote(HttpServletRequest request,String app) {
		request.setAttribute("app", app);  
		return "cms/vote/addVote";
	}
	
	@RequestMapping(value = "/toAddVoteItem.cms")
	public String toAddVoteItem(HttpServletRequest request,String voteId,String voteItemId) throws AppBusinessException {
		Vote vote=voteBiz.getVoteById(voteId);
		VoteItem voteItem=voteBiz.getVoteItemById(voteItemId);
		request.setAttribute("vote", vote); 
		request.setAttribute("voteItem", voteItem);
		return "cms/vote/addVoteItem";
	}

	@RequestMapping(value = "/saveVote.cms")
	public String saveVote(HttpServletRequest request,HttpServletResponse response, Vote vote) throws IOException {
		response.setContentType("text/html; charset=UTF-8");
		try {
			voteBiz.saveVote(vote);
			response.getWriter().write("success");
		} catch (AppBusinessException e) {
			if (logger.isErrorEnabled()) {
				logger.error("MVC failure.", e);
			}
		}
		return null;
	}
	
	
	@RequestMapping(value = "/getVoteItem.cms")
	public String toVoteItem(HttpServletRequest request,Integer page,String app,String voteType) {
		try {
			PageModel pm=voteBiz.getVote(page, app,voteType);
			request.setAttribute("pm", pm);
			request.setAttribute("app", app);
			request.setAttribute("page", page);
		} catch (AppBusinessException e) {
			if (logger.isErrorEnabled()) {
				logger.error("MVC failure.", e);
			}
		}
		return "cms/vote/voteItem";
	}

	//结束
	@RequestMapping(value = "/closeVote.cms")
	public String closeVote(HttpServletRequest request,
			HttpServletResponse response, String voteId) {
		try {
			response.setContentType("text/html; charset=UTF-8");
			PrintWriter pw = response.getWriter();
			voteBiz.closeVote(voteId);
			pw.print("success");
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("MVC failure.", e);
			}
		}
		return null;
	}
	//结束
	@RequestMapping(value = "/openVote.cms")
	public String openVote(HttpServletRequest request,
			HttpServletResponse response, String voteId) {
		try {
			response.setContentType("text/html; charset=UTF-8");
			PrintWriter pw = response.getWriter();
			voteBiz.openVote(voteId);
			pw.print("success");
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("MVC failure.", e);
			}
		}
		return null;
	}

	//删除投票项
	@RequestMapping(value = "/deleteVote.cms")
	public String deleteVote(HttpServletRequest request,
			HttpServletResponse response, String voteId,Integer sort,String app) {
		try {
			response.setContentType("text/html; charset=UTF-8");
			PrintWriter pw = response.getWriter();
			voteBiz.deleteVoteAndChangeSort(voteId, sort,app);
			pw.print("success");
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("MVC failure.", e);
			}
		}
		return null;
	}

	//删除投票项
	@RequestMapping(value = "/deleteVoteItem.cms")
	public String deleteVoteItem(HttpServletRequest request,
			HttpServletResponse response, String voteItemId) {
		try {
			response.setContentType("text/html; charset=UTF-8");
			PrintWriter pw = response.getWriter();
			voteBiz.deleteVoteItem(voteItemId);
			pw.print("success");
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("MVC failure.", e);
			}
		}
		return null;
	}
	
	@RequestMapping(value = "/getVoteById.cms")
	public String getVoteById(HttpServletRequest request, String id) {
		try {
			Vote vote = voteBiz.getVoteById(id);
			request.setAttribute("vote", vote);
			request.setAttribute("app", vote.getApp());
		} catch (AppBusinessException e) {
			if (logger.isErrorEnabled()) {
				logger.error("MVC failure.", e);
			}
		}
		return "cms/vote/addVote";
	}

	// 上移
	@RequestMapping(value = "/addVoteSort.cms")
	public String addVoteSort(HttpServletRequest request,
			HttpServletResponse response, String voteId, Integer sort,String app) {
		try {
			response.setContentType("text/html; charset=UTF-8");
			PrintWriter pw = response.getWriter();
			/*Vote vote = new Vote();
			vote.setIdentifier(voteId);
			vote.setSort(sort);
			vote.setApp(app);*/
			voteBiz.moveVoteSortUp(voteId);

			pw.write("success");
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("MVC failure.", e);
			}
		}
		return null;
	}

	// 下移
	@RequestMapping(value = "/downVoteSort.cms")
	public String downVoteSort(HttpServletRequest request,
			HttpServletResponse response, String voteId, Integer sort,String app) {
		try {
			response.setContentType("text/html; charset=UTF-8");  
			PrintWriter pw = response.getWriter();
			/*Vote vote = new Vote(); 
			vote.setIdentifier(voteId);
			vote.setSort(sort);
			vote.setApp(app);*/
			voteBiz.moveVoteSortDown(voteId);
			pw.write("success");
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("MVC failure.", e);
			}
		}
		return null;
	}

	// 添加列项
	@RequestMapping(value = "/saveVoteItem.cms")
	public String saveVoteItem(HttpServletRequest request,
			HttpServletResponse response, VoteItem voteItem) {
		try {
			response.setContentType("text/html; charset=UTF-8");
			PrintWriter pw = response.getWriter();
			voteBiz.saveVoteItem(voteItem);
			pw.write("success");
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("saveVoteItem failure.", e);
			}
		}
		return null;
	}

	// 添加列项
	@RequestMapping(value = "/getVoteItemById.cms")
	public String getVoteItemById(HttpServletRequest request,
			HttpServletResponse response, String voteItemId) {
		try {
			response.setContentType("text/html; charset=UTF-8");
			PrintWriter pw = response.getWriter();
			VoteItem voteItem = voteBiz.getVoteItemById(voteItemId);
			pw.print(JSONArray.fromObject(voteItem));
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("MVC failure.", e);
			}
		}
		return null;
	}
	
	//上传图片
	@RequestMapping(value = "/voteUploadImg.cms")
	public String voteUploadImg(HttpServletRequest request,HttpServletResponse response) {
		try {
			response.setContentType("text/html; charset=UTF-8");
			response.setHeader("Cache-Control", "no-cache"); 
			PrintWriter pw = response.getWriter();
			PictureUrl pictureUrl = ImageUtil.uploadImage(request, "vote");
			pw.print(JSONObject.fromObject(pictureUrl));
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("MVC failure.", e);
			}
		}
		return null;
	}
	
}
